# -*- coding:utf8 -*-
# __author__ = "zhangying"
# __date__ = "2019/3/11 14:23"
#注册页面 http://my.caissa.com.cn/Registered/index
import sys
sys.path.append('D:/seleniumpython')
from selenium import webdriver
import time
import random
from PIL import Image
from ShowapiRequest import ShowapiRequest
from find_element import FindElement


class RegisterFunction(object):

    def __init__(self,url):
        self.driver = self.get_driver(url)

    #获取driver并且打开url
    def get_driver(self,url):
        driver = webdriver.Chrome()
        driver.get(url)
        driver.maximize_window()
        return driver

    #输入用户信息-填写值
    def send_user_info(self,key,data):
        self.get_user_element(key).send_keys(data)

    #定位用户注册信息，获取value
    def get_user_element(self,key):
        find_element = FindElement(self.driver)
        user_element = find_element.get_element(key)
        return user_element

    #获取图片
    def get_code_image(self,file_name):
        self.driver.save_screenshot(file_name)
        code_element = self.get_user_element("code_image")
        left = code_element.location['x']
        top = code_element.location['y']
        right = code_element.size['width']+left
        height = code_element.size['height']+top
        im = Image.open(file_name)
        img = im.crop((left,top,right,height))
        img.save(file_name)

     #解析图片获取验证码
    def code_online(self,file_name):
        self.get_code_image(file_name)
        r = ShowapiRequest("http://route.showapi.com/184-4","62626","d61950be50dc4dbd9969f741b8e730f5" )
        r.addBodyPara("typeId", "34")
        r.addBodyPara("convert_to_jpg", "0")
        r.addBodyPara("needMorePrecise", "0")
        r.addFilePara("image", file_name) #文件上传时设置
        res = r.post()
        text = res.json()['showapi_res_body']['Result']
        return text

   #获取图片
    def get_code_image2(self,file_name2):
        self.driver.save_screenshot(file_name2)
        code_element = self.get_user_element("code_image")
        left = code_element.location['x']
        top = code_element.location['y']
        right = code_element.size['width']+left
        height = code_element.size['height']+top
        im = Image.open(file_name2)
        img = im.crop((left,top,right,height))
        img.save(file_name2)

     #解析图片获取验证码
    def code_online2(self,file_name2):
        self.get_code_image(file_name2)
        r = ShowapiRequest("http://route.showapi.com/184-4","62626","d61950be50dc4dbd9969f741b8e730f5" )
        r.addBodyPara("typeId", "34")
        r.addBodyPara("convert_to_jpg", "0")
        r.addBodyPara("needMorePrecise", "0")
        r.addFilePara("image", file_name2) #文件上传时设置
        res = r.post()
        text = res.json()['showapi_res_body']['Result']
        return text



    def main(self):
        file_name = "D:/seleniumpython/code_image.png"
        code_text = self.code_online(file_name)
        self.send_user_info("phone","13524559846")
        time.sleep(1)
        self.send_user_info("code",code_text)
        code_error = self.get_user_element("code_error")
        if code_error == None:
            print("验证码正确")
        else:
            self.driver.save_screenshot("D:/seleniumpython/codeerror.png")
        time.sleep(5)
        self.get_user_element("phone_code_click").click()
        time.sleep(5)
        file_name2 = "D:/seleniumpython/code_image2.png"
        code_text2 = self.code_online2(file_name2)
        self.send_user_info("code",code_text2)
        code_error = self.get_user_element("code_error")
        if code_error == None:
            print("验证码正确")
        else:
            self.driver.save_screenshot("D:/seleniumpython/codeerror.png")
        self.send_user_info("phone_code","5310")
        phone_code_error = self.get_user_element("phone_code_error")
        if phone_code_error == None:
            print("短信验证码正确")
        else:
            self.driver.save_screenshot("D:/seleniumpython/phonecodeerror.png")
        self.send_user_info("password","qiao2012")
        self.send_user_info("confirm_password","qiao2012")
        self.get_user_element("register_button").click()
        self.driver.close()
       

if __name__ == "__main__":
    register_function = RegisterFunction("http://my.caissa.com.cn/Registered/index")
    register_function.main()


    
    


